package edu.auctionmart

import groovyx.net.http.HTTPBuilder
import org.joda.time.DateTime

class SmsService {

	void sendSMS(String phoneNum, Auction theAuction, User user) {
		//should send different SMS messages depending on whether auction is over or not
		println("Sending an SMS message")
		
		

		Auction auction = Auction.get(theAuction.id)

		Set<Bid> bids = auction.getBids()
		
		DateTime now = DateTime.now()
		DateTime end = auction.getEndTime()

		String winningUsername = ""
		String seller = ""

		if (now >= end) { //auction has ended, current bid should be the winning bid
			println(bids.size())

			if (auction.getWinner() == null && bids.size() > 0) { //first time setting winner
				println("Setting winner")
				Bid winningBid = auction.getCurrentBid()
				User winningUser = winningBid.getUserAccount()
				auction.setWinner(winningUser)
				auction.save(flush: true)
			}

			if (auction.getWinner() != null) {
				winningUsername = auction.getWinner().getUsername()
				if (user.getUsername() == winningUsername || user.getUsername() == "admin") {
					sendWonSMS(auction, phoneNum)
					return
				}
			}

			if (user.getUsername() == auction.getSeller().getUsername()) {
				if (auction.getWinner() != null) {
					sendSoldSMS(auction, phoneNum)
					return
				} else {
					sendUnsoldSMS(auction, phoneNum)
					return
				}
			}

			//Here the auction ended, the user had an e-mail alert, but they did not win
			sendDidNotWin(auction, phoneNum)
			return
		} else { //Informational notification
			sendInfo(auction, phoneNum, user)
			return
		}
	}

	void sendWonSMS(Auction auction, String phoneNum) {
		auction.refresh()
		String auctionTitle = auction.getAuctionTitle()
		float auctionPrice = auction.getCurrentBid().getAmount()
		String message = "Congratulations, you won the auction for " + auctionTitle + " at " + auctionPrice
		request(message, phoneNum)
		

	}

	void sendSoldSMS(Auction auction, String phoneNum) {
		auction.refresh()
		String auctionTitle  = auction.getAuctionTitle()
		float auctionPrice = auction.getCurrentBid().getAmount()
		String message = "Congratulations, your auction for " + auctionTitle + " sold for " + auctionPrice
		request(message, phoneNum)

	}

	void sendUnsoldSMS(Auction auction, String phoneNum ) {
		auction.refresh()
		String auctionTitle = auction.getAuctionTitle()
		String message = "Sorry, your auction for " + auctionTitle + " did not sell"
		request(message, phoneNum)

	}

	void sendDidNotWin(Auction auction, String phoneNum) {
		auction.refresh()
		String auctionTitle = auction.getAuctionTitle()
		String message = "Sorry, you did not win the auction for " + auctionTitle
		request(message, phoneNum)

	}

	void sendInfo(Auction auction, String phoneNum, User username) {
		auction.refresh()
		String auctionTitle = auction.getAuctionTitle()
		Float currentPrice = auction?.getCurrentBid()?.getAmount()
		String message 
		if (currentPrice) {
			message = "The auction for " + auctionTitle + " is at " + currentPrice + " and " + auction.getCurrentBid().getUserAccount().getUsername() + " is winning"
		} else {
		 message = "The auction for " + auctionTitle + " is at " + auction.getStartBid() + " and there are no bids"
		}
		request(message, phoneNum)
	}
	
	private void request(String message, String phoneNum) {
		def http = new HTTPBuilder('http://api.messaging.staging.voxeo.net')
		http.auth.basic 'SSA_Grameen', 'NXT8TrubEJj7YMBvNUo9iE310'
		def response = http.get( path : '/1.0/messaging', query : [user: phoneNum, msg: message, botkey: '291784', apimethod: 'send', network: 'SMS', from: '8572848721'] )
	}
}

